package com.himiotech.core.project.web.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.himiotech.core.framework.helper.anno.Log;
import com.himiotech.core.framework.helper.constant.UserConstants;
import com.himiotech.core.framework.helper.enums.BusinessType;
import com.himiotech.core.framework.helper.utils.ExcelUtil;
import com.himiotech.core.project.model.entity.dict.DictData;
import com.himiotech.core.project.model.entity.dict.DictType;
import com.himiotech.core.project.service.IDictDataService;
import com.himiotech.core.project.service.IDictTypeService;
import com.himiotech.core.project.web.base.HimioBaseController;
import com.himiotech.core.project.web.base.model.HimioResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author wuwenbin
 **/
@Controller
@RequestMapping("/system/dict")
public class DictController extends HimioBaseController {

    @Autowired
    private IDictTypeService dictTypeService;
    @Autowired
    private IDictDataService dictDataService;

    @RequiresPermissions("system:dict:view")
    @GetMapping
    public String type() {
        return "system/dict/dict";
    }

    @RequiresPermissions("system:dict:view")
    @GetMapping("/data")
    public String data(Model model, String dictType) {
        model.addAttribute("dictType", dictType);
        return "system/dict/dictData";
    }

    /**
     * 查询字典类型表列表
     *
     * @param page     分页信息
     * @param dictType 查询条件
     * @return 表格分页数据
     */
    @RequiresPermissions("system:dict:list")
    @PostMapping("/list")
    @ResponseBody
    public HimioResult list(Page<DictType> page, DictType dictType) {
        handleOrderBy(request, page);
        page = dictTypeService.selectDictTypeListPage(page, dictType);
        return writeJsonLayuiTable(page);
    }

    /**
     * 导出字典类型表列表数据
     *
     * @param dictType 查询条件
     * @return 导出数据
     */
    @Log(title = "字典类型表 - 导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    @ResponseBody
    public HimioResult export(DictType dictType) {
        Page<DictType> page = new Page<>(1, 65535);
        page = dictTypeService.selectDictTypeListPage(page, dictType);
        ExcelUtil<DictType> util = new ExcelUtil<>(DictType.class);
        return util.exportExcel(page.getRecords(), "字典类型表");
    }


    /**
     * 新增保存字典类型
     */
    @Log(title = "字典类型 - 添加", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public HimioResult addSave(@Validated DictType dict) {
        if (UserConstants.DICT_TYPE_NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return writeJsonErr("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return writeJson(dictTypeService.insertDictType(dict));
    }


    /**
     * 修改保存字典类型
     */
    @Log(title = "字典类型 - 修改", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public HimioResult editSave(@Validated DictType dict) {
        if (UserConstants.DICT_TYPE_NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return writeJsonErr("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return writeJson(dictTypeService.updateDictType(dict));
    }

    @Log(title = "字典类型 - 移除", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove")
    @ResponseBody
    public HimioResult remove(String ids) {
        dictTypeService.deleteDictTypeByIds(ids);
        return writeJsonOk();
    }

    /**
     * 清空缓存
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型 - 清除缓存", businessType = BusinessType.CLEAN)
    @GetMapping("/refreshCache")
    @ResponseBody
    public HimioResult refreshCache() {
        dictTypeService.resetDictCache();
        return writeJsonOk();
    }

    /**
     * 校验字典类型
     */
    @PostMapping("/checkDictTypeUnique")
    @ResponseBody
    public String checkDictTypeUnique(DictType dictType) {
        return dictTypeService.checkDictTypeUnique(dictType);
    }


    /**
     * 字典数据列表（不分页）
     *
     * @param page     分页信息
     * @param dictData 查询
     * @return
     */
    @RequiresPermissions("system:dict:list")
    @PostMapping("/data/list")
    @ResponseBody
    public HimioResult list(Page<DictData> page, DictData dictData) {
        handleOrderBy(request, page);
        page = dictDataService.selectDictDataListPage(page, dictData);
        return writeJsonLayuiTable(page);
    }

    /**
     * 导出字典类型表列表数据
     *
     * @param dictData 查询条件
     * @return 导出数据
     */
    @Log(title = "字典类型表 - 导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/data/export")
    @ResponseBody
    public HimioResult dataExport(DictData dictData) {
        Page<DictData> page = new Page<>(1, 65535);
        page = dictDataService.selectDictDataListPage(page, dictData);
        ExcelUtil<DictData> util = new ExcelUtil<>(DictData.class);
        return util.exportExcel(page.getRecords(), "字典数据表");
    }


    /**
     * 新增保存字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/data/add")
    @ResponseBody
    public HimioResult addDictData(@Validated DictData dict) {
        return writeJson(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/data/edit")
    @ResponseBody
    public HimioResult editDictData(@Validated DictData dict) {
        return writeJson(dictDataService.updateDictData(dict));
    }

    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/data/remove")
    @ResponseBody
    public HimioResult removeDictData(String ids) {
        dictDataService.deleteDictDataByIds(ids);
        return writeJsonOk();
    }

}
